在计算机科学中, 指针 是一种基本的 间接形式。指针变量并不直接保存值,而是保存该值的 内存地址——即内存中的具体位置——值被存储的位置。这使得程序能够在不进行昂贵的数据复制的情况下,协调对单一数据源的修改。
1. 地址的逻辑
值所存储的位置被称为其 内存地址。理解这一点是掌握计算机内部语言的第一步。在 Go 中,我们使用与号(&)来获取地址,使用星号(*)来跟随它。
2. 为什么间接性很重要
间接性是构建复杂共享数据结构的强大工具。想象一个商店招牌指引访客前往新地址。招牌本身并不包含商店;它告诉你 去哪里 寻找。Go 提供了一个安全的环境来掌握这一概念:如果你之前接触过指针,请深呼吸。这不会太糟糕。如果你是第一次接触,放松吧。Go 是学习指针的安全之地。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Like the shop sign directing visitors to a new address, pointers direct a computer where to look for a value. What's another situation where you're directed to look somewhere else?
A URL redirecting to a new website.
Reading the final page of a book directly.
Calculating 2 + 2 in your head.
A static variable that never changes.
✅ Correct!
Correct! URLs, library index cards, and table of contents are all real-world forms of indirection.❌ Incorrect
Think about things that act as references to other locations rather than the content itself.QUESTION 2
How do you know that
time.Time never uses a pointer receiver?It is an immutable type that returns a new value for every method call.
It is a primitive type like int.
The Go compiler prohibits pointers on time structs.
It uses global variables instead.
✅ Correct!
Exactly. Methods like .Add return a new time.Time instead of modifying the existing one, which is characteristic of value receivers.❌ Incorrect
Check the documentation: methods return a new instance of Time rather than mutating the current one.QUESTION 3
What is the result of using the address operator (
&) on a variable?It returns the value stored in the variable.
It returns the memory address (location) of the variable.
It doubles the memory usage of the variable.
It deletes the variable from memory.
✅ Correct!
The ampersand is the 'address-of' operator.❌ Incorrect
The address operator finds the 'where', not the 'what'.QUESTION 4
Quick check 26.1: If a pointer is a signpost, what happens if the building it points to is painted a different color? Does the signpost need to change?
Yes, because the signpost must describe the color.
No, because the address (location) remains the same.
Yes, the pointer automatically breaks.
No, pointers only point to grayscale buildings.
✅ Correct!
Correct. Changing the data at the address doesn't change the address itself.❌ Incorrect
The pointer only cares about the location, not the internal state of the data stored there.QUESTION 5
Which statement best describes Go's approach to pointers?
It allows dangerous pointer arithmetic like C.
It is a safe environment that handles many complexities for you.
Pointers are not allowed in Go.
Pointers only work with integers.
✅ Correct!
Go is designed to be a safe place to learn and use pointers without many of the pitfalls of lower-level languages.❌ Incorrect
Review the 'Tips' section: Go is a safe place to learn pointers.Module 7 Challenge: Indirection and Sudoku Logic
Applying Pointers to Shared State Management
You are tasked with building a Sudoku validation engine. In Sudoku, the grid must be shared across various validation rules (row, column, subgrid). Passing the entire 81-element array by value would be inefficient. You must use pointer receivers to mutate the grid and handle errors gracefully.
Q
1. Write a line of code to sort a slice named 'food' from the shortest to longest string using 'sort.Slice' and a closure. [Word Count Requirement: 15 words]
Solution:
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
Q
2. If an error occurred while writing 'Clear is better than clever.' to a file in Listing 28.6, what sequence of events would follow?
Solution:
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
Q
3. Implement a method signature to clear a digit from a square in a Sudoku struct. This method need not adhere to constraints, as several squares may be empty (zero).
Solution:
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }